/**
 * 添加或者修改页面
 */
var PositionInfoDlg = {
    data: {
        fId: "",
        fName: "",
        fContent: "",
        fTag: "",
        remark: "",
        fKeyWords: "",
    }
};

layui.use(['form', 'admin', 'ax', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var upload = layui.upload;
    var form = layui.form;
    var admin = layui.admin;

    //实例化编辑器
    var ue = UE.getEditor('container', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/fileRun/imgUpdate';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/ueditor/uploadfile';
        } else if (action === 'uploadvideo') {
            return Feng.ctxPath + '/ueditor/uploadvideo';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };

    //技术栈标签
    $.ajax({
        url: Feng.ctxPath +'/fileTag/getTaglist/2' ,
        type: 'get',
        dataType: 'json',
        success: function (res) {
            var roleList = res.records;
            console.log(roleList)
            //遍历所有的拿到的复选框数据
            for (i = 0; i < roleList.length; i++) {
                //如果有就渲染选中的模板
                $('#taglist').append(' <input type="checkbox" name="tag" lay-filter="like" title="' + roleList[i].tagName + '" value="' + roleList[i].id + '" lay-skin="primary">')
            }
            //初始化数据
            form.render();
        },
        error: function (e) {
            layer.msg('连接超时，请稍后再试');
        }
    });

    //项目整体分类
    $.ajax({
        url: Feng.ctxPath +'/fileTag/getTaglist/1' ,
        type: 'get',
        dataType: 'json',
        success: function (res) {
            var roleList = res.records;
            console.log(roleList)
            //遍历所有的拿到的复选框数据
            for (i = 0; i < roleList.length; i++) {
                //如果有就渲染选中的模板
                $("#category").append("<option value="+roleList[i].id+">"+roleList[i].tagName+"</option>");
            }
            form.render();
        },
        error: function (e) {
            layer.msg('连接超时，请稍后再试');
        }
    });

    //执行实例
    var uploadInst = upload.render({
        elem: '#fileBtn' //绑定元素
        , url: Feng.ctxPath + '/fileRun/upload' //上传接口
        , accept: 'file'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $("#fileNameTip").html(file.name);
            });
        }
        , done: function (res) {
            $("#fUrl").val(res.data.fileUrl);
            Feng.success(res.message);
        }
        , error: function () {
            Feng.error("上传文件失败！");
        }
    });

    // //多图片上传
    // var imagesUrl = '';
    // var uploadsInst = upload.render({
    //     elem: '#filesBtn'
    //     , url: Feng.ctxPath + '/fileRun/uploads'
    //     , multiple: true
    //     , before: function (obj) {
    //         obj.preview(function (index, file, result) {
    //             $('#demo2').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="92px" height="92px" style="margin-right: 5px">')
    //         });
    //     }
    //     , done: function (res) {
    //         var loading = layer.load(0, {
    //             shade: [0.1, '#fff'],
    //             time: 5 * 1000
    //         });
    //         //上传完毕
    //         if (res.code === 0) {
    //             layer.close(loading);
    //             imagesUrl += res.data.images
    //             $("#imagesUrl").val(imagesUrl);
    //         } else {
    //             layer.close(loading);
    //             Feng.error("上传文件失败！");
    //         }
    //     }
    //     , error: function () {
    //         // Feng.error("上传文件失败！");
    //     }
    // });
    var tagStr = '';
    //表单提交事件
    form.on('submit(btnSubmit)',function (data) {
        $('input[type=checkbox]:checked').each(function() {
            tagStr+=$(this).val()+","
        });
        console.log(tagStr);
        data.field.fTag=tagStr;
        var category = $('select[name="category"] option:selected').val();
        console.log(category);
        data.field.fCategory=category;
        var ajax = new $ax(Feng.ctxPath + "/fileRun/addItem", function (data) {
            Feng.success("添加成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
});